﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using System.IO.IsolatedStorage;
using System.IO;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using GameStateManagement;
using Microsoft.Phone.Shell;
using System.Xml.Serialization;

namespace PiratesOfBermuda
{
    public class SinglePlayerGameProcessor : GameProcessor
    {
        public SinglePlayerGameProcessor(Game game, SpriteBatch spriteBatch)
            : base(game, spriteBatch)
        {
        }

        public void InitGame()
        {
            GeneratePlayer(ref player, 5, 5, "Single"); // @DuyQuang: code con cung, can sua lai
            player.LifeRemained = playerLifeRemained = 2;
            GenerateGameObjects();
        }

        public void LoadGameObject(string key, int x, int y)
        {
            switch ((String)PhoneApplicationService.Current.State[key])
            {
                case "Player":
                    GeneratePlayer(ref player, x, y, "Single");
                    break;
                case "Enemy":
                    BasicEnemy newAI;
                    newAI = new BasicEnemy(game, spriteBatch, x, y);
                    newAI.Initialize();
                    enemies.Add(newAI);
                    gameObjects.Add(x.ToString() + "_" + y.ToString(), newAI);
                    break;
                case "Whirl":
                    Whirl newWhirl;
                    newWhirl = new Whirl(game, spriteBatch, x, y);
                    newWhirl.Initialize();
                    whirls.Add(newWhirl);
                    gameObjects.Add(x.ToString() + "_" + y.ToString(), newWhirl);
                    break;
                case "Island":
                    Island newIsland = new Island(game, spriteBatch, x, y);
                    islands.Add(newIsland);
                    newIsland.Initialize();
                    gameObjects.Add(x.ToString() + "_" + y.ToString(), newIsland);
                    break;
                case "Rubble":
                    Rubble newRubble = new Rubble(game, spriteBatch, x, y);
                    newRubble.Initialize();
                    newRubble.IsActive = true;
                    rubbles.Add(newRubble);
                    gameObjects[key] = newRubble;
                    break;
            }
        }

        #region UPDATE
        public override void Update(GameTime gameTime)
        {
            //Neu luot nguoi choi da xong
            //Luot nguoi choi ket thuc khi tau chuyen ve tinh trang Standby
            if (player.CurrentShipState == ShipState.Standby && isPlayerTurn && !player.IsActive)
            {
                isPlayerTurn = false;
                foreach (BasicEnemy enemy in enemies)
                {
                    enemy.IsActive = true;
                    Point nextMove = enemy.GetNextMove(player.X, player.Y);
                    enemy.Move(nextMove.X, nextMove.Y);
                }
                if (enemies.Count != 0)
                    AudioManager.PlaySound("MoveEvil");
            }

            base.Update(gameTime);
            UpdatePlayer(gameTime);
        }

        protected override void UpdateEnemies(GameTime gameTime)
        {
            //Neu la luot cua AI
            if (!isPlayerTurn)
            {
                if (handleEnemiesUpdateEvent())
                {
                    isPlayerTurn = true;
                    player.IsActive = true;
                }
            }
            //update tung enemy nhu binh thuong
            foreach (BasicEnemy enemy in enemies)
            {
                enemy.Update(gameTime);
            }
        }

        #endregion

        #region DRAW

        protected override void CheckAndDrawAnimations()
        {
            if (!isPlayerTurn && isDone)
            {
                DrawExplodeAnimations();
            }
        }
        #endregion

        #region Player Helper Method
        public override void HandleInput(List<GestureSample> gestures)
        {
            if (isPlayerTurn && gameState == GameState.Playing)
            {
                HandleUserInput(gestures, null);
            }
        }
        #endregion
    }
}